package ${package.Service}.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import ${package.Entity}.${entity};
import ${package.Mapper}.${entity}Mapper;
import ${package.Service}.${entity}Service;
import com.baoyouqun.domain.VO.${entity}VO;
import com.baoyouqun.domain.CreateCmd.${entity}CreateCmd;
import com.baoyouqun.domain.UpdateCmd.${entity}UpdateCmd;
import com.baoyouqun.domain.Query.${entity}Query;
import org.springframework.beans.BeanUtils;
import com.baoyouqun.base.utils.ConvertUtils;

import java.util.List;
import java.util.Objects;


@Service
public class ${entity}ServiceImpl extends ServiceImpl<${entity}Mapper, ${entity}> implements ${entity}Service {

    @Resource
    private ${entity}Mapper ${entity?uncap_first}Mapper;

    @Override
    public SingleResponse<${entity}VO> create(${entity}CreateCmd cmd) {
    ${entity} entity = ConvertUtils.copyBean(cmd, entity);
        Boolean result = save(entity);
        if (!result) {
            return SingleResponse.buildFailure("100101", "创建失败");
        }

        ${entity}VO vo = ConvertUtils.copyBean(entity, ${entity}VO.class);
        return SingleResponse.of(vo);
    }

    @Override
    public Response update(${entity}UpdateCmd cmd) {
        if (Objects.isNull(cmd)) {
            return Response.buildFailure("100301", "参数不能为空");
        }
        if (StringUtil.isBlank(cmd.getId())) {
            return Response.buildFailure("100302", "ID不能为空");
        }
    ${entity} oldEntity = getById(cmd.getId());
        if (Objects.isNull(oldEntity)) {
            return Response.buildFailure("100303", "未查询到此数据");
        }
        oldEntity = ConvertUtils.copyBean(cmd, ${entity}. class);
        Boolean result = updateById(oldEntity);
        if (result) {
            return Response.buildSuccess();
        } else {
            return Response.buildFailure("100304", "更新失败");
        }
    }

    @Override
    public ${entity}VO selectById(String id) {
    ${entity} entity = ${entity?uncap_first}Mapper.selectById(id);
        ${entity}VO vo = new ${entity}VO();
        ConvertUtils.copyBean(entity, vo);
        return vo;
    }

    @Override
    public IPage<${entity}VO> pageVo(${entity}Query query) {
        IPage<${entity}> page = new Page<${entity}>(query.getCurrent(), query.getSize());
        page = page(page, new LambdaQueryWrapper<${entity}>());
        Page<${entity}VO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<${entity}VO> records = ConvertUtils.copyBeanList(page.getRecords(), ${entity}VO.class);
        voPage.setRecords(records);
        return voPage;
    }
}